#!/usr/bin/env python3
"""
安装项目依赖的脚本
"""

import subprocess
import sys

def install_package(package_name):
    """安装指定的包"""
    try:
        print(f"正在安装 {package_name}...")
        subprocess.check_call([sys.executable, "-m", "pip", "install", package_name])
        print(f"✅ {package_name} 安装成功")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ {package_name} 安装失败: {e}")
        return False

def check_package(package_name):
    """检查包是否已安装"""
    try:
        __import__(package_name)
        print(f"✅ {package_name} 已安装")
        return True
    except ImportError:
        print(f"❌ {package_name} 未安装")
        return False

def main():
    """主函数"""
    print("检查和安装项目依赖...")
    print("=" * 50)
    
    # 必需的依赖
    required_packages = [
        ("PySide6", "PySide6"),
        ("PIL", "Pillow"),
        ("pyautogui", "pyautogui"),
        ("qt_material", "qt-material"),
    ]
    
    # 可选的依赖（用于全局热键）
    optional_packages = [
        ("pynput", "pynput"),
    ]
    
    print("检查必需依赖:")
    all_required_installed = True
    for import_name, package_name in required_packages:
        if not check_package(import_name):
            if install_package(package_name):
                print(f"重新检查 {import_name}...")
                check_package(import_name)
            else:
                all_required_installed = False
    
    print("\n检查可选依赖:")
    for import_name, package_name in optional_packages:
        if not check_package(import_name):
            print(f"安装可选依赖 {package_name} (用于全局热键功能)...")
            install_package(package_name)
    
    print("\n" + "=" * 50)
    if all_required_installed:
        print("✅ 所有必需依赖已安装完成")
        print("现在可以运行程序: python app.py")
    else:
        print("❌ 部分必需依赖安装失败，请手动安装")
    
    # 检查全局热键功能
    try:
        import pynput
        print("✅ 全局热键功能可用")
    except ImportError:
        print("⚠️  全局热键功能不可用，如需使用请安装: pip install pynput")

if __name__ == "__main__":
    main()
